package com.example.demo.thread;

/**
 * @Author: xiatian
 * @CreateTime: 2021/9/16 10:21 上午
 * @Description:
 */
public class GetProcessThreadDemo {
    public static void main(String[] args) {
//        System.out.println("HELLO WORLD");
//        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
//        long[] threadIds = threadMXBean.getAllThreadIds();
//        ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadIds);
//        for (ThreadInfo threadInfo : threadInfos) {
//            System.out.println(threadInfo.getThreadId()+": "+threadInfo.getThreadName());
//        }
//    }

        System.out.println("hello world");
        ThreadGroup group = Thread.currentThread().getThreadGroup();
        ThreadGroup topGroup = group;
        while (group != null) {
            topGroup = group;
            group = group.getParent();
        }
        int nowThreads = topGroup.activeCount();
        Thread[] lstThreads = new Thread[nowThreads];
        topGroup.enumerate(lstThreads);
        for (int i = 0; i < nowThreads; i++) {
            System.out.println("线程number:" + i + " = " + lstThreads[i].getName());
        }
    }
}
